package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.RegisteredItem;
import com.bjsxt.dto.RegisteredItemDto;
import com.bjsxt.mapper.RegisteredItemMapper;
import com.bjsxt.service.RegisteredItemService;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 挂号费用service接口实现类
 */
@Service
public class RegisteredItemServiceImpl implements RegisteredItemService {

    @Autowired
    private RegisteredItemMapper registeredItemMapper;

    /**
     * 分页查询挂号费用
     *
     * @param registeredItemDto
     * @return
     */
    @Override
    public DataGridView listRegisteredItemPage(RegisteredItemDto registeredItemDto) {
        Page<RegisteredItem> page = new Page<>(registeredItemDto.getPageNum(), registeredItemDto.getPageSize());
        QueryWrapper<RegisteredItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(registeredItemDto.getRegItemName()), RegisteredItem.COL_REG_ITEM_NAME, registeredItemDto.getRegItemName());
        queryWrapper.eq(StringUtils.isNotBlank(registeredItemDto.getStatus()), RegisteredItem.COL_STATUS, registeredItemDto.getStatus());
        this.registeredItemMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 根据registeredItemId查询挂号费用
     *
     * @param registeredItemId
     * @return
     */
    @Override
    public RegisteredItem getOne(Long registeredItemId) {
        return this.registeredItemMapper.selectById(registeredItemId);
    }

    /**
     * 添加挂号费用
     *
     * @param registeredItemDto
     * @return
     */
    @Override
    public int addRegisteredItem(RegisteredItemDto registeredItemDto) {
        RegisteredItem registeredItem = new RegisteredItem();
        BeanUtil.copyProperties(registeredItemDto, registeredItem);
        registeredItem.setCreateBy(registeredItemDto.getSimpleUser().getUserName());
        registeredItem.setCreateTime(DateUtil.date());
        return this.registeredItemMapper.insert(registeredItem);
    }

    /**
     * 修改挂号费用
     *
     * @param registeredItemDto
     * @return
     */
    @Override
    public int updateRegisteredItem(RegisteredItemDto registeredItemDto) {
        RegisteredItem registeredItem = new RegisteredItem();
        BeanUtil.copyProperties(registeredItemDto, registeredItem);
        registeredItem.setUpdateBy(registeredItemDto.getSimpleUser().getUserName());
        registeredItem.setUpdateTime(DateUtil.date());
        return this.registeredItemMapper.updateById(registeredItem);
    }

    /**
     * 根据registeredItemIds删除挂号费用
     *
     * @param registeredItemIds
     * @return
     */
    @Override
    public int deleteRegisteredItemByIds(Long[] registeredItemIds) {
        List<Long> ids = Arrays.asList(registeredItemIds);
        if (ids.size() > 0) {
            return this.registeredItemMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    /**
     * 查询所有可用的挂号项目
     *
     * @return
     */
    @Override
    public List<RegisteredItem> queryAllRegisteredItems() {
        QueryWrapper<RegisteredItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RegisteredItem.COL_STATUS, Constants.STATUS_TRUE);
        return this.registeredItemMapper.selectList(queryWrapper);
    }
}
